fix: allow C-implemented built-ins (e.g. dict, int) as Optional defaults#349
Open
Labib-Bin-Salam wants to merge 1 commit into
Open
Conversation
`_invoke_with_optional_kwargs` called `inspect.signature(f)` on the
default callable, but that raises `ValueError` for many C-implemented
built-ins (e.g. `dict`, `int`). This made them unusable as `Optional`
defaults, so
Schema({Optional("k", default=dict): dict}).validate({})
raised instead of returning `{"k": {}}`.
Catch the introspection failure and fall back to calling the default
with no arguments -- the only sensible call for such callables, and the
behaviour they had before validate() keyword arguments were forwarded to
defaults. Callables that do expose a signature are unaffected, so
keyword-argument forwarding still works.
Closes keleshev#272.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #272.
Problem
_invoke_with_optional_kwargs(added in #268 to forwardvalidate()keyword arguments to callableOptionaldefaults) callsinspect.signature(f)unconditionally. For many C-implemented built-ins —dict,int, and others —inspect.signatureraisesValueError: no signature found for builtin type, so those callables can no longer be used as defaults:(Exactly which built-ins are affected varies by Python version — on 3.13
dictandintstill raise, on older versions more do — but the breakage has been present since 0.7.5.)Fix
Catch the introspection failure and fall back to calling the default with no arguments. That is the only sensible call for a callable whose signature cannot be inspected, and it restores the behaviour these defaults had before keyword-argument forwarding was introduced. Callables that do expose a signature are untouched, so kwarg forwarding still works:
Notes
test_optional_callable_default_builtin_c_callables) that also assertsvalidate()kwargs are still forwarded to introspectable callables.CHANGELOG.mdto gitchangelog (matching recent fixes such as Fix TypeError building key error message for a tuple key (#253) #347).pytest, 124 tests);ruffandruff-format(v0.4.3) clean.Optionalfailing whendefaultis some builtins #276, deliberately avoiding the kwargs-forwarding redesign that stalled that PR.